import { ADD_TAB, CURRENT, OTHER,CURRENT_AND_LINK } from "../types/tabs";
import router from "@/router";
export default {
  namespaced: true,
  state: {
    tabs: [
        {
            key: 1,
            name: "首页",
            path: "/",
            isClose: false,
          }
        
    ],
    tabSet: ['/'],
    // {
    //     // '/':true,
    //     '/home/12':true
    // }
  },
  mutations: {
    [ADD_TAB](state, data) {
      state.tabSet.push(data.path),
      state.tabs.push({
        key: new Date().getTime(),
        name: data.meta || data.meta.name || "",
        path: data.path,
        isClose: true,
      });
    },
    [CURRENT](state, path) {
      state.tabs = state.tabs.filter((item) =>(!item.isClose) || item.path !== path);
      if(state.tabs.length === state.tabSet.length){
        return;
      }
      router.push(state.tabs[state.tabs.length - 1].path);
      state.tabSet = state.tabs.map(item=>item.path);
    },
    [CURRENT_AND_LINK](state, data) {
      const {path,toPath} = data
      state.tabs = state.tabs.filter((item) =>(!item.isClose) || item.path !== path);
      if(state.tabs.length === state.tabSet.length){
        return;
      }
      router.push(toPath);
      state.tabSet = state.tabs.map(item=>item.path);
    },
    [OTHER](state, path) {
      state.tabs = state.tabs.filter((item) =>(!item.isClose) || item.path === path);
      state.tabSet = state.tabs.map(item=>item.path);
    },
  },
  actions: {
    [ADD_TAB]({ commit }, data) {
      commit(ADD_TAB, data);
    },
    [CURRENT]({ commit }, path) {
      commit(CURRENT, path);
    },
    [OTHER]({ commit }, path) {
      commit(OTHER, path);
    },
    [CURRENT_AND_LINK]({commit},data){
      commit(CURRENT_AND_LINK,data)
    }
  },
};
